package com.fivestar.finance.app.ui.message;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.MotionEvent;
import android.view.View;

import com.fivestar.finance.app.R;
import com.fivestar.finance.app.bean.AuthorInfo;
import com.fivestar.finance.app.ui.BaseFragment;
import com.fivestar.finance.app.ui.main.MainComponent;
import com.fivestar.finance.library.widget.LoadMoreRecyclerView;
import com.fivestar.finance.library.widget.pulltorefresh.PullToRefreshView;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 简要描述
 *  
 *  @author Derek.Kidd
 * @version V1.0.0
 * @date 2016/8/23 10:53
 */
public class MessageFragment extends BaseFragment implements PullToRefreshView.OnRefreshListener,
        LoadMoreRecyclerView.LoadMoreListener, IMessageView {

    @BindView(R.id.recycler_view)
    LoadMoreRecyclerView recyclerView;

    @BindView(R.id.refreshLayout)
    PullToRefreshView refreshLayout;

    private AuthorRecyclerAdapter mRecyclerAdapter;

    private List<AuthorInfo> mAuthorInfoList;

    @Inject
    MessagePrecenter messagePrecenter;

    @Override
    public void initInjector() {
        getComponent(MainComponent.class).inject(this);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_message;
    }

    @Override
    public void getBundle(Bundle bundle) {
        mAuthorInfoList = AuthorInfo.createTestData();
    }

    @Override
    public void initUI(View view) {
        messagePrecenter.attachView(this);

        recyclerView.setLoadingMore(true);
        refreshLayout.setOnRefreshListener(this);

        mRecyclerAdapter = new AuthorRecyclerAdapter(getActivity(), mAuthorInfoList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setLoadMoreListener(this);

        mRecyclerAdapter.setSmallType(false);
        recyclerView.setAdapter(mRecyclerAdapter);
        //ItemTouchHelper 用于实现 RecyclerView Item 拖曳效果的类
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                //actionState : action状态类型，有三类 ACTION_STATE_DRAG （拖曳），ACTION_STATE_SWIPE（滑动），ACTION_STATE_IDLE（静止）
                int dragFlags = makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP | ItemTouchHelper.DOWN
                        | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);//支持上下左右的拖曳
                int swipeFlags = makeMovementFlags(ItemTouchHelper.ACTION_STATE_SWIPE, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);//表示支持左右的滑动
                return makeMovementFlags(dragFlags, swipeFlags);//直接返回0表示不支持拖曳和滑动
            }

            /**
             * @param recyclerView attach的RecyclerView
             * @param viewHolder 拖动的Item
             * @param target 放置Item的目标位置
             * @return
             */
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                int fromPosition = viewHolder.getAdapterPosition();//要拖曳的位置
                int toPosition = target.getAdapterPosition();//要放置的目标位置
                mRecyclerAdapter.moveItem(fromPosition, toPosition);
                return true;
            }

            /**
             * @param viewHolder 滑动移除的Item
             * @param direction
             */
            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();//获取要滑动删除的Item位置
                mRecyclerAdapter.removeItem(position);
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return super.isLongPressDragEnabled();//不支持长按拖曳效果直接返回false
            }

            @Override
            public boolean isItemViewSwipeEnabled() {
                return super.isItemViewSwipeEnabled();//不支持滑动效果直接返回false
            }
        });
//        itemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
    }

    @Override
    public void initData() {
        messagePrecenter.getData();
    }


    @Override
    public void onRefresh() {

        messagePrecenter.onRefresh();
        List<AuthorInfo> list = AuthorInfo.createTestData();
        mAuthorInfoList.clear();
        mAuthorInfoList.addAll(list);
        recyclerView.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void showLoading() {
        refreshLayout.setRefreshing(true);
    }

    @Override
    public void showContent() {
        showContent(true);
    }

    @Override
    public void notifyRecyclerView(List<AuthorInfo> authLists) {
        mAuthorInfoList.addAll(authLists);
        mRecyclerAdapter.notifyItemInserted(mRecyclerAdapter.getItemCount());
    }

    @Override
    public void onError(String error) {
        setErrorText(error);
        showError(true);
    }

    @Override
    public void onEmpty() {
        setEmptyText("没有数据");
        showEmpty(true);
    }

    @Override
    public void onLoadCompleted(boolean hasMore) {
        recyclerView.onLoadCompleted(hasMore);
    }

    @Override
    public void onRefreshCompleted() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onReloadClicked() {
        messagePrecenter.onReload();
    }

    @Override
    public void onLoadMore() {
        messagePrecenter.onLoadMore();
    }

}
